package mini;

import java.util.HashMap;
import java.util.LinkedList;

import mini.statement.AssignStatement;
import mini.statement.Statement;

public class StatementList {

    private LinkedList<Statement> list;

    /**
     * This is the ability to create an empty statement
     * list. It really should be rarely used; currently
     * only used when creating a procedure by passing
     * in a string of code.
     */
    protected StatementList() {
		list = new LinkedList<Statement>();
    }
    
    public StatementList(Statement st){
		//System.out.println(st);
		list = new LinkedList<Statement>();
		list.add(st);
    }

    public void eval(HashMap hm, HashMap<String, Proc> functiontable, LinkedList var){
		int i;
		int l = list.size();
		Statement s;
	
		for (i=0;i<l;i++){
		    s = (Statement)list.get(i);
		    s.eval(hm, functiontable, var);
		}
    }

    public String parse(HashMap<String,Integer> hm, HashMap<String, Proc> functiontable, LinkedList var){
		int i;
		int l = list.size();
		Statement s;
		
		StringBuilder sb = new StringBuilder();
	
		for (i=0;i<l;i++){
		    s = (Statement)list.get(i);
		    sb.append(s.parse(hm,functiontable,var));
		    sb.append( Program.NEWLINE );
		}
		
		return sb.toString();
    }

    public void insert(Statement s){
		list.add(s);
    }

    public LinkedList<Statement> getStatements(){
		return list;
    }

	@Override
	public String toString() {
	
		StringBuilder sb = new StringBuilder();
	
		for (Object o : list) {
			if (o instanceof AssignStatement) {
				AssignStatement as = (AssignStatement)o;
				sb.append(as.toString());
			} else {
				sb.append("unhandled statement item... add to toString()");
			}
		}
		
		return sb.toString();
	}
}
